package ws.very.web.adptor

import java.util.AbstractMap
import scala.collection.convert.WrapAsJava
import scala.collection.convert.WrapAsScala
import javax.servlet.http.HttpServletRequest
import java.util.LinkedHashSet

class WebSessionMap(req: HttpServletRequest) extends AbstractMap[String, Object] with SessionMap with WrapAsScala with WrapAsJava {

  protected lazy val session = req.getSession(true)

  override def containsKey(key: Object) =
    session.getAttribute(key.toString()) != null

  override def get(key: Object) =
    session.getAttribute(key.toString())

  override def put(key: String, value: Object) =
    {
      val old = get(key)
      session.setAttribute(key.toString(), value)
      old
    }
  override def remove(key: Object) =
    {
      val old = get(key)
      session.removeAttribute(key.toString)
      old
    }
  //)java.util.Set[java.util.Map.Entry[String,Object]] 
  override def entrySet = {
    val t = new LinkedHashSet[java.util.Map.Entry[String, Object]]
    session.getAttributeNames.foreach { k =>
      t.add(new java.util.Map.Entry[String, Object] {
        def getKey = k.toString
        def getValue = session.getAttribute(k.toString)
        def setValue(v: Object) = put(k.toString, v)

      })
    }
    t
  }
}